
//express_demo.js 文件
var express = require('express');
var app = express();

app.get('/api/user/:path', function (req, res) {
    //res.set({'Content-Type':'application/html','Connection':'close'});

   // console.log(res.get(''));


    res.format({
        'text/plain':function()
        {
            res.send('it is text/plain');
        },
        'text/html':function()
        {
            res.send('it is text/html');
        },
        'application/html':function()
        {
            var filename=__dirname +'\\'+req.query.path;
            console.log('filename:',filename);


            //var filename=__dirname + '/index.html';

            res.sendFile(filename,function(err)
            {
                if(err)
                {
                    res.status(404).json({messsage:'it is json',else:'it is else'});
                    console.error(err);

                }
                else
                {
                    console.log('send:',filename);
                }


            });
            //res.status(500).json({messsage:'it is json',else:'it is else'});
        },
        'default':function()
        {
             res.status(403).send('it is default');

        }

    });
    //res.send('Hello World');
    // console.log(req.route);
    // console.log('oriURL:',req.originalUrl);
    // console.log('params:',req.params);
    // console.log('from:%s:%s',req.hostname,req.ip);
    // console.log('head:',req.get('Content-Type'));
    // console.log('head:',req.get('Accept-Encoding'));
    // console.log('body:',req.body);
    // console.log('cookies',req.cookies);
    // console.log('query:',req.query);
});

app.post('/', function (req, res) {
    //res.send('Hello World');
    console.log('post:/');
    res.sendFile(__dirname + '/index.html');
   // console.log(req.route);
});

// app.param(['id','page'],function(req,res,next,value)
// {
//     console.log('called with ',value);
//     next();
// });

app.get('/user/:id/:page', function(req,res){
    console.log('get:/');
    console.log(req.params.id,req.params.page);
    console.log(req.query.id);
    res.end();
   // res.sendFile(__dirname + '/index.html');
});

var server = app.listen(8888, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

})